package process

import (
	"fmt"
	"time"
)

//type IGlobalConfig interface {
//	Check() error
//}
//
//type IConfig interface {
//	ID() int
//	SetID(id int)
//	Check() error
//}

const ConfigName = "process"

type ServiceConfig struct {
	Name string
	ID   int
}

type Config struct {
	id                              int
	PprofIP                         string          // pprof监听IP，禁止用公网IP
	PprofPort                       int             // pprof监听端口
	EndpointIP                      string          // RPC监听IP，禁止用公网IP
	EndpointPort                    int             // RPC监听端口
	Services                        []ServiceConfig // 承载的服务，按顺序启动
	LogDir                          string          // 日志目录
	StaticDataDir                   string          // 静态表目录
	StartServiceIntervalMillisecond time.Duration   // 启动服务的间隔毫秒
	StopServiceIntervalMillisecond  time.Duration   // 停止服务的间隔毫秒
	ExitWaitSecond                  time.Duration   // 进程退出等待秒数
}

func (c Config) ID() int {
	return c.id
}

func (c *Config) SetID(id int) {
	c.id = id
}

func (c *Config) Check() error {
	if c.id <= 0 {
		return fmt.Errorf("invalid Process.ID")
	}
	if len(c.Services) == 0 {
		return fmt.Errorf("no service")
	}
	if c.StartServiceIntervalMillisecond < 100 {
		return fmt.Errorf("Process.StartServiceIntervalMillisecond must >= 100")
	}
	if c.StopServiceIntervalMillisecond < 100 {
		return fmt.Errorf("Process.StopServiceIntervalMillisecond must >= 100")
	}
	if c.ExitWaitSecond < 5 {
		return fmt.Errorf("Process.ExitWaitSecond must >= 5")
	}
	return nil
}
